package com.cskaoyan.service;

import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.marketingmanagementbean.ReadVO;
import com.cskaoyan.bean.marketingmanagementbean.TopicData;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.MarketTopic;
import com.cskaoyan.bean.MarketTopicExample;
import com.cskaoyan.bean.wx.home.vo.TopicVo;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建日期: 2022/05/06 17:46
 *
 * @author liutao
 */

@Service
public class TopicServiceImpl implements TopicService{

    @Autowired
    MarketTopicMapper marketTopicMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public TopicData topicList(BaseParam baseParam, String title, String subtitle) {
        Integer page = baseParam.getPage();
        Integer limit = baseParam.getLimit();
        String sort = baseParam.getSort();
        String order = baseParam.getOrder();
        PageHelper.startPage(page, limit);

        MarketTopicExample example = new MarketTopicExample();
        example.setOrderByClause(sort + " " + order);
        MarketTopicExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (title != null && !"".equals(title)) {
            criteria.andTitleLike("%" + title + "%");
        }
        if (subtitle != null && "".equals(subtitle)) {
            criteria.andSubtitleLike("%" + subtitle + "%");
        }
        List<MarketTopic> marketTopicList = marketTopicMapper.selectByExampleWithBLOBs(example);

        PageInfo<MarketTopic> pageInfo = new PageInfo<>(marketTopicList);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return TopicData.data(total, pages, limit, page, marketTopicList);
    }

    @Override
    public void topicCreate(MarketTopic marketTopic) {
        int insert = marketTopicMapper.insert(marketTopic);
    }

    @Override
    public void topicDelete(MarketTopic marketTopic) {
        int i = marketTopicMapper.updateByPrimaryKey(marketTopic);
    }

    @Override
    public ReadVO topicRead(String id) {
        ReadVO readVO = new ReadVO();
        MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(Integer.valueOf(id));
        Integer[] goods = marketTopic.getGoods();
        ArrayList<MarketGoods> marketGoodsArrayList = new ArrayList<>();
        for (Integer good : goods) {
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(good);
            marketGoodsArrayList.add(marketGoods);
        }
        readVO.setTopic(marketTopic);
        readVO.setGoodsList(marketGoodsArrayList);
        return readVO;
    }

    @Override
    public void batchDelete(Integer[] ids) {

        for (Integer id : ids) {
            MarketTopic marketTopic = new MarketTopic();
            marketTopic.setDeleted(true);
            MarketTopicExample example = new MarketTopicExample();
            MarketTopicExample.Criteria criteria = example.createCriteria();
            criteria.andIdEqualTo(id);
            marketTopicMapper.updateByExampleSelective(marketTopic, example);
        }
    }

    @Override
    public List<TopicVo> queryAllTopic() {
        MarketTopicExample example = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        ArrayList<TopicVo> topicVos = new ArrayList<>();
        for (MarketTopic marketTopic : marketTopicMapper.selectByExample(example)) {
            TopicVo topicVo = new TopicVo();
            topicVo.set(marketTopic);
            topicVos.add(topicVo);
        }
        return topicVos;
    }

    @Override
    public void update(MarketTopic marketTopic) {
        MarketTopicExample example = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(marketTopic.getId());
        marketTopicMapper.updateByExampleSelective(marketTopic, example);
    }

    @Override
    public MarketTopic queryTopic(Integer id) {
        MarketTopicExample example = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExampleWithBLOBs(example);
        return marketTopics.get(0);
    }
}
